import { mapState, mapActions } from 'vuex';
import { queryTypeRecordAll } from '../api/typeRecord.js';
import dictMixin from '@/mixin/dictMixin';
const kyTypeMixin =  {
    data() {
        return {
            kyTypeArr:[]
        };
    },
    mixins:[dictMixin],
    computed: {
        
    },
    watch: {
        
    },
    mounted() {
        
    },
    methods: {
        getKyType(){
            this.getDictByName('kyType').then(({data}) => {
                this.kyTypeArr = this.getLevelData(data,1)
            });
        },
        //类型
        handleCodeType(dictid) {
            return this.getNameById(dictid,this.kyTypeArr)
        },
        //方向
        handleDirection(dictid) {
            let dict = this.getDictParent(dictid,this.kyTypeArr);
            return dict.dictname;
        },
        getDirectionWithCodeType(dictid) {
            let dict = this.getDictParent(dictid,this.kyTypeArr);
            return dict.dictid;
        },
        //方向/类型
        handleCodeTypeAndDirection(val) {
            return this.handleDictSepShow(val,this.kyTypeArr);
        },
        //通过方向获取类型
        filterTypeWithDirection(name) {
            return this.getDictChildren(name,this.kyTypeArr);
        },
    },
};

const kySourceMixin =  {
    data() {
        return {
            kySourceArr:[]
        };
    },
    mixins:[dictMixin],
    computed: {
        
    },
    watch: {
        
    },
    mounted() {
        
    },
    methods: {
        getKySource(){
            this.getDictByName('xmly').then(({data}) => {
                this.kySourceArr = this.getLevelData(data,1)
            });
        }
    },
};

const typeRecordMixin = {
    mixins:[],
    data(){
        return {
            typeRecordData:[]
        }
    },
    created() {},
    methods: {
        queryTypeRecordAll() {
            queryTypeRecordAll()
                .then(({ code, data, message }) => {
                    if (code == 1) {
                        let typeRecordData = data;
                        typeRecordData.forEach((dict) => {
                            dict.key = dict.id;
                            dict.label = dict.typeName;
                            dict.value = dict.id;
                            dict.name = dict.typeName;
                        });
                        this.typeRecordData = typeRecordData;
                    }
                })
                .catch((error) => {
                    this.$Message.error('获取内置类型错误，请稍后再试!');
                })
                .finally(() => {
                    
                });
        },
        handleTypeRecord(id) {
            let record = this.typeRecordData.find((item) => item.id === id);
            return record ? record.name : '-';
        },
    },
};
import { getFlavor, getImage } from '../api/resource.js';
const imageFloverMixin = {
    data(){
        return {
            imageList:[], 
            flavorList:[]
        }
    },
    created() {
        
    },
    methods: {
        handleImageName(id) {
            let image = this.imageList.find((item) => item.id === id);
            return image ? image.name : '-';
        },
        handleFloverName(id) {
            let flavor = this.flavorList.find((item) => item.id === id);
            return flavor ? flavor.name : '-';
        },
        //获取所有镜像
        async getImageList() {
            await getImage({ pageIndex: 1, pageSize: 2147483647 }).then((res) => {
                if (res.code == 1) {
                    this.imageList = res.data.items
                }
            });
        },
        //获取所有配额数据
        async getFlavorList() {
            await getFlavor({}, { pageIndex: 1, pageSize: 2147483647 }).then((res) => {
                if (res.code == 1) {
                    this.flavorList = res.data.items
                }
            });
        },
        getAvaiFlavorList(image) {
            if (!image.minRam) {
                return this.flavorList;
            }
            return this.flavorList.filter(
                (item) => item.ram > Number(image.minRam) && item.disk > Number(image.minDisk)
            );
        },
    },
};
const visibleMixin = {
    data() {
        return {
            visibleArr: [
                {
                    key: '0',
                    value: '个人',
                    color: 'rgb(255, 165, 0)'
                },
                {
                    key: '1',
                    value: '单位',
                    color: 'rgb(50, 205, 50)'
                },
                {
                    key: '2',
                    value: '系统',
                    color: 'rgb(65, 105, 225)'
                }
            ],
            visibleArrString: [
                {
                    key: 'private',
                    value: '个人',
                    color: 'rgb(255, 165, 0)'
                },
                {
                    key: 'share',
                    value: '单位',
                    color: 'rgb(50, 205, 50)'
                },
                {
                    key: 'public',
                    value: '系统',
                    color: 'rgb(65, 105, 225)'
                }
            ]
        };
    },
    computed: {
        allVisibleList() {
            return [{ key: 'all', value: '全部' }, ...this.visibleArr];
        },
        allVisibleListString() {
            return [{ key: 'all', value: '全部' }, ...this.visibleArrString];
        }
    },
    methods: {
        filterVisible(val, type) {
            let typeInfo =
                this.visibleArr.find((type) => type.key === val) ||
                this.visibleArrString.find((type) => type.key === val);
            if (type === 'color') {
                return typeInfo ? typeInfo.color : '';
            } else {
                return typeInfo ? typeInfo.value : '未定义';
            }
        }
    }
};
export { typeRecordMixin, imageFloverMixin, visibleMixin, kyTypeMixin, kySourceMixin };
